<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Creating Communicating Processes
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot127.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot124.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc287">20.4</A>&nbsp;&nbsp;Creating Communicating Processes</H2><UL>
<LI><A HREF="umsroot128.html#toc161">Process creation</A>
<LI><A HREF="umsroot128.html#toc162">Process control</A>
<LI><A HREF="umsroot128.html#toc163">Interprocess Signals</A>
</UL>

ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> provides all the necessary built-ins needed to create UNIX processes
and establish communication between them.
A ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> process can communicate with other processes via streams and by
sending and receiving signals.<BR>
<BR>
<A NAME="toc161"></A>
<H3 CLASS="subsection"><A NAME="htoc288">20.4.1</A>&nbsp;&nbsp;Process creation</H3>
<A NAME="@default1143"></A>
<A NAME="@default1144"></A>
<A NAME="@default1145"></A>
<A NAME="@default1146"></A>
The built-ins of the <B>exec</B> group and <A HREF="../bips/kernel/opsys/sh-1.html"><B>sh/1</B></A><A NAME="@default1147"></A>
fork a new process and execute the command given as the first argument.
Sorted by their versatility, there are:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<B>sh(Command)</B>
<LI CLASS="li-itemize"><B>exec(Command, Streams)</B>
<LI CLASS="li-itemize"><B>exec(Command, Streams, ProcessId)</B>
<LI CLASS="li-itemize"><B>exec_group(Command, Streams, ProcessId)</B>
</UL>
<A NAME="@default1148"></A>
With <A HREF="../bips/kernel/opsys/sh-1.html"><B>sh/1</B></A><A NAME="@default1149"></A> (or its synonym <A HREF="../bips/kernel/opsys/system-1.html"><B>system/1</B></A><A NAME="@default1150"></A>) it is possible to
call and execute any UNIX command from withing ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>.
However it is not possible to communicate with the process.
Moreover, the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> process just waits until the command has been executed.<BR>
<BR>
The <B>exec</B> group makes it possible to set up communication links
with the child process by specifying the <TT>Streams</TT> argument.
It is a list of the form
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[Stdin, Stdout, Stderr]
</PRE></BLOCKQUOTE>
and specifies which ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> stream should be connected to the
<I>stdin</I>, <I>stdout</I> or <I>stderr</I> of the child respectively.
Unless <TT>null</TT> is specified, this will establish pipes to be
created between the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> process and the child.
On Berkeley UNIX systems the streams can be specified as <I>sigio(Stream)</I>
which will setup the pipe such that the signal <I>sigio</I> is issued
every time new data appears on the pipe.
Thus, by defining a suitable interrupt handler, it is possible to service this
stream in a completely asynchronous way.<BR>
<BR>
<A NAME="toc162"></A>
<H3 CLASS="subsection"><A NAME="htoc289">20.4.2</A>&nbsp;&nbsp;Process control</H3>
The <A HREF="../bips/kernel/opsys/sh-1.html"><B>sh/1</B></A><A NAME="@default1151"></A> and <A HREF="../bips/kernel/opsys/exec-2.html"><B>exec/2</B></A><A NAME="@default1152"></A> built-ins both block the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> process until
the child has finished.
For more sophisticated applications, the processes have to run in parallel
and be synchronised explicitly.
This can be achieved with <A HREF="../bips/kernel/opsys/exec-3.html"><B>exec/3</B></A><A NAME="@default1153"></A> or <A HREF="../bips/kernel/opsys/exec_group-3.html"><B>exec_group/3</B></A><A NAME="@default1154"></A>.
These return immediately after having created the child process and
unify its process identifier (<I>Pid</I>) with the their argument.
The Pid can be used to 
<UL CLASS="itemize"><LI CLASS="li-itemize">
send signals to the process, using the built-in <B>kill(Pid, Signal)</B>
<LI CLASS="li-itemize">wait for the process to terminate and obtain its return status
<B>wait(Pid, Status)</B>
</UL>
<A NAME="@default1155"></A>
<A NAME="@default1156"></A>
The difference between <A HREF="../bips/kernel/opsys/exec-3.html"><B>exec/3</B></A><A NAME="@default1157"></A> and <A HREF="../bips/kernel/opsys/exec_group-3.html"><B>exec_group/3</B></A><A NAME="@default1158"></A> is
that the latter creates a
new process group for the child, such that the child does not get the
interrupt, hangup and kill signals that are sent to the parent.<BR>
<BR>
The process identifier of the running ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> and of its parent process are
available as the global flags <B>pid</B> and <B>ppid</B> respectively.
<A NAME="@default1159"></A>
<A NAME="@default1160"></A>
<A NAME="@default1161"></A>
They can be accessed using <A HREF="../bips/kernel/env/get_flag-2.html"><B>get_flag/2</B></A><A NAME="@default1162"></A> or <A HREF="../bips/kernel/env/env-0.html"><B>env/0</B></A><A NAME="@default1163"></A>.<BR>
<BR>
Here is an example of how to connect the UNIX utility <B>bc</B> (the
arbitrary-precision arithmetic language) to a ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> process.
We first create the process with two pipes for the child's standard input
and output.
Then, by writing and reading these streams, the processes can communicate in
a straightforward way. Note that it is usually necessary to flush the
output after writing into a pipe:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse 1]: exec(bc, [in,out], P).

P = 9759
yes.
[eclipse 2]: writeln(in, "12345678902321 * 2132"), flush(in).

yes.
[eclipse 3]: read_string(out, "\n", _, Result).

Result = "26320987419748372"
yes.
</PRE></BLOCKQUOTE>
In this example the child process can be terminated by closing its standard
input (in other cases it may be necessary to send a signal).
The built-in <A HREF="../bips/kernel/opsys/wait-2.html"><B>wait/2</B></A><A NAME="@default1164"></A> is then used to wait for the process to terminate
and to obtain its exit status.
Don't forget to close the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> streams that were opend by <A HREF="../bips/kernel/opsys/exec-3.html"><B>exec/3</B></A><A NAME="@default1165"></A>:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse 4]: close(in), wait(P,S).

P = 9759
S = 0     More? (;) 
yes.
[eclipse 5]: at_eof(out), close(out).

yes.
</PRE></BLOCKQUOTE>
<A NAME="toc163"></A>
<H3 CLASS="subsection"><A NAME="htoc290">20.4.3</A>&nbsp;&nbsp;Interprocess Signals</H3>
The UNIX (or the appropriate Windows) signals are all mapped to ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> interrupts.
Their names and numbers may vary on different machines.
Refer to the operating system documentation for details.<BR>
<BR>
The way to deal with incoming signals is to define a Prolog or external
predicate and declare it as the interrupt handler for this interrupt
(using <A HREF="../bips/kernel/event/set_interrupt_handler-2.html"><B>set_interrupt_handler/2</B></A><A NAME="@default1166"></A>).
Interrupt handlers can be established for all signals except those that are
not allowed to be caught by the process (like e.g. the <I>kill</I> signal 9).
For a description of event handling in general see chapter <A HREF="umsroot070.html#chapexcept">13</A>.<BR>
<BR>
<A NAME="@default1167"></A>
For explicitly sending signals to other processes <A HREF="../bips/kernel/opsys/kill-2.html"><B>kill/2</B></A><A NAME="@default1168"></A> is provided,
which is a direct interface to the UNIX system call <I>kill(2)</I>.
Note that some signals can be set up to be raised automatically,
e.g. <TT>sigio</TT> can be raised when data arrives on a pipe.<BR>
<BR>
<HR>
<A HREF="umsroot127.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot124.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
</BODY>
</HTML>
